package com.kanshu.ksgb.fastread.doudou.module.bookcity.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdStyleBean;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.TopicListAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedRequestParams;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 精品专题页面
 * Created by wcy on 18/5/24
 */
@Route(path = ARouterConfig.TOPIC_LIST_ALL)
public class TopicListActivity extends BaseActivity implements IGenrialMvpView<BaseResult<List<SelectedBean>>> {
    @BindView(R.id.recyler_view)
    RecyclerView mRecylerView;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    SelectedRequestParams mRequestParams;
    BookCityPresenter mBookCityPresenter;
    TopicListAdapter mAdapter;
    List<BookInfo> mBookInfos = new ArrayList<>();
    private FrameLayout mAdContainer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("精品专题");
        setContentView(R.layout.layout_common_refresh_rv);
        ButterKnife.bind(this);
        init();
    }

    private void addAdInfo() {
        mAdContainer = new FrameLayout(getActivity());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mSwipeRefresh.addView(mAdContainer, params);
        AdUtils.fetchAdUtil(getActivity(), mAdContainer, null, ADConfigBean.POSITION_TOPIC_BOTTOM_ZT_1, AdStyleBean.BANNER_STYLE, 0, null);
    }

    private void init() {
        mBookCityPresenter = new BookCityPresenter(lifeCyclerSubject);
        mRequestParams = new SelectedRequestParams();
        mBookCityPresenter.setMvpView(this);
        SwipeRefreshHelper.init(mSwipeRefresh, () -> {
            mRequestParams.page = 1;
            mBookCityPresenter.getTopics(mRequestParams);
        });
        mAdapter = new TopicListAdapter(this, mBookInfos);
        //mAdapter.addFooterView(BookCityUtils.addEmptyFooter(this, DisplayUtils.dip2px(this, 56), 0));
        RecyclerViewHelper.initRecyclerViewV(this, mRecylerView, mAdapter);
        SelectedRequestParams params = new SelectedRequestParams();
        params.type_name = Constants.BookCityModuleType.TYPE_SELECTED_ZHUANTI;
        params.num = 8;
        mAdapter.setNoMoreDataDesc("爱着你的我也是有底线的");
        mRequestParams = params;
        mBookCityPresenter.getTopics(params);
        mAdapter.setOnItemClickListener((view, position) -> {
            position = position - mAdapter.getHeaderViewsCount();
            BookInfo item = mBookInfos.get(position);
            Map<String, String> map = new HashMap<>();
            map.put("title", item.title);
            map.put("id", item.id);
            ARouterUtils.toActivity(ARouterConfig.BOOK_MENU_DETAIL, map);
        });
        addAdInfo();
        mAdapter.setIsNoMoreData(true);
        mAdapter.setRequestDataListener(() -> mBookCityPresenter.getTopics(mRequestParams));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBookCityPresenter.detachView();
        AdUtils.destroyAd(mAdContainer);
    }

    @Override
    public void showLoading(String s) {

    }

    @Override
    public void dismissLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void showContent(BaseResult<List<SelectedBean>> result) {
        dismissLoading();
        if (mRequestParams.page == 1) {
            if (Utils.isEmptyList(result.result.data) && mBookInfos.size() <= 0) {
                if (mEmptyLayout == null) {
                    return;
                }
                showEmptyByCode(Constants.ErrCode.NoData);
                SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
                return;
            }
            mBookInfos.clear();
            mAdapter.setIsNoMoreData(false);
        }
        mBookInfos.addAll(result.result.data.get(0).list);
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        if (mRequestParams.page >= result.result.data.get(0).page_info.total_page) {
            mAdapter.noMoreData();
        } else {
            mRequestParams.page++;
            mAdapter.setIsNoMoreData(false);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(int errCode, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        if (Utils.isEmptyList(mBookInfos)) {
            showEmptyByCode(errCode);
        }
    }

    public void showEmptyByCode(int code) {
        int status = EmptyLayout.STATUS_NO_NET;
        if (code == Constants.ErrCode.NoData) {
            status = EmptyLayout.STATUS_NO_DATA;
        }
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(status);
        }
    }
}
